﻿using HRMSystem.DBServices;
using HRMSystem.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace HRMSystem.Wpf.WpfWindow
{
    public partial class LoginWindow : Window
    {
        List<Employee> userList;

        private static LoginWindow instance;

        private LoginWindow()
        {
            InitializeComponent();

            CompanyName.Content = ApplicationSettings.COMPANY_SHORT_NAME;

            CompanyName.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#Dekers");
            CompanyNameHR.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#Dekers");
        }

        public static LoginWindow Instance
        {
            get
            {
                if (instance == null || !instance.IsActive)
                {
                    instance = new LoginWindow();
                }

                return instance;
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            string username = UserNameTextBox.Text;
            string password = PasswordTextBox.Password;

            //dev
            username = "admin";
            password = "admin";

            try
            {
                userList = EmployeeService.GetLoggedEmployeeByUserNamePassword(username, password);

                if (userList.Count() == 1)
                {
                    Session.LoggedEmployee = userList.SingleOrDefault();
                    Session.AdminState = userList.SingleOrDefault().hrm_titles.SingleOrDefault(t => t.ISCURRENT == true).POSITION.GetValueOrDefault();
                    Session.CompanyID = userList.SingleOrDefault().hrm_titles.SingleOrDefault(t => t.ISCURRENT == true).com_departments.com_branches.COMPANY_ID;
                    Session.BranchID = userList.SingleOrDefault().hrm_titles.SingleOrDefault(t => t.ISCURRENT == true).com_departments.BRANCH_ID;
                    Session.DepartmentID = userList.SingleOrDefault().hrm_titles.SingleOrDefault(t => t.ISCURRENT == true).DEPARTMENT_ID;
                    
                    Session.NewEmployeeID = EmployeeService.CountAllEmployees() + 1;

                    MainWindow.Instance.Show();
                    this.Close();
                }
                else
                {
                    MessageWindow ew = new MessageWindow("Login Error", "Please check your username and password");
                    ew.Show();
                    UserNameTextBox.Clear();
                    PasswordTextBox.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageWindow ew = new MessageWindow("Login Error", ex.Message.ToString());
                ew.Show();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            UserNameTextBox.Clear();
            PasswordTextBox.Clear();
        }
    }
}
